<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
	<title>Offline</title>

	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="icon" type="image/png" sizes="174x174" href="./favicon.png">

	<script src="https://unpkg.com/@webcomponents/webcomponentsjs@^2/webcomponents-bundle.js"></script>
	<script src="../build/Tone.js"></script>
	<script src="./js/tonejs-ui.js"></script>
	<style type="text/css">
		tone-button {
			margin-bottom: 10px;
		}
	</style>
</head>
<body>
	<tone-example>
		<tone-loader></tone-loader>

		<tone-explanation label="Offline Rendering">
			Tone.Offline renders a chunk of Tone.js code into an AudioBuffer. An offline instance of Tone.Transport is passed into the callback which can be used to schedule events. It may take a moment to render the sound. 
			<br><br>
			<a href="https://tonejs.github.io/docs/Tone#offline-static">Tone.Offline</a> docs.
		</tone-explanation>

		<tone-content>
			<tone-button label="Render"></tone-button>
			<tone-play-toggle disabled></tone-play-toggle>
		</tone-content>
	</tone-example>

	<script type="text/javascript">
		//load the buffer for use in the convolver
		var buffer = new Tone.Buffer("./audio/IRs/chorus-feedback.wav");

		function generateAudioOffline(){
			return Tone.Offline(function(Transport){

				var reverb = new Tone.Convolver(buffer).toMaster();
				reverb.wet.value = 0.2;

				var pannerA = new Tone.Panner(-1).connect(reverb);
				var synthA = new Tone.Synth({
					"envelope" : {
						"attack" : 0.01,
						"decay" : 5,
						"sustain" : 0
					},
					"oscillator" : {
						"type" : "sawtooth4"
					}
				}).connect(pannerA);
				var seqA = new Tone.Sequence(function(time, note){
					synthA.triggerAttack(note, time);
				}, ["A4", "G4", "G#4", "F#4", "E4"], "8n").start(0);
				seqA.loop = false;

				var pannerB = new Tone.Panner(1).connect(reverb);
				var synthB = new Tone.Synth({
					"envelope" : {
						"attack" : 0.001,
						"decay" : 3,
						"sustain" : 0
					},
					"oscillator" : {
						"type" : "square8"
					}
				}).connect(pannerB);
				var seqB = new Tone.Sequence(function(time, note){
					synthB.triggerAttack(note, time);
				}, ["G#4", "A4", "G4", "F4", "C4"], "8n").start("16n");
				seqB.loop = false;

				var bass = new Tone.MonoSynth({
					"envelope" : {
						"attack" : 0.01,
						"decay" : 3,
						"sustain" : 0.1
					},
				}).toMaster();
				var bassSeq = new Tone.Sequence(function(time, note){
					bass.triggerAttackRelease(note, "1n", time);
				}, ["C2", "C2", "F1", "F1"], "4n").start(0);
				bassSeq.loop = false;

				Transport.bpm.value = 150;
				Transport.start();
			}, 7);
		}

		//play the buffer with a Tone.Player when it's been generated
		var player = new Tone.Player().toMaster();

		//bind the interface
		document.querySelector("tone-button").addEventListener("click", e => {
			e.target.setAttribute("label", "Rendering...");
			e.target.setAttribute("disabled", "");
			var buffer = generateAudioOffline().then(buffer => {
				document.querySelector("tone-button").setAttribute("label", "Rendered");
				player.buffer = buffer;
				document.querySelector("tone-play-toggle").removeAttribute("disabled");
			});
		});
		document.querySelector("tone-play-toggle").bind(player);

	</script>
</body>
</html>
